<template>
  <div class="inventoryPosting">
    <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <el-button type="primary" size="mini" style="margin-left: 0" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
      />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  INVENTORY_info
} from '@/api/financial/salesMargin'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import { maxDecimal } from '@/utils/number'
import filtration from '@/components/filtration'
export default {
  name: 'InventoryPosting',
  components: {
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 16,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('shop.type'), prop: 'lx' },
        { label: '业务区域', prop: 'salCY' },
        { label: '销售单号', prop: 'soNum' },
        { label: this.$t('market.paezText1'), prop: 'khdd' },
        { label: this.$t('market.client'), prop: 'kh' },
        { label: '业务员', prop: 'saler' },
        { label: this.$t('market.fmodel'), prop: 'xh' },
        { label: '是否套件', prop: 'tz' },
        { label: this.$t('market.fmaterial'), prop: 'itemcode', minWidth: '130px' },
        { label: this.$t('shop.name1'), prop: 'fName' },
        { label: this.$t('market.descrip'), prop: 'itemName', minWidth: '200px' },
        { label: '销售订单数量', prop: 'salquantity' },
        { label: '累计出库数', prop: 'cKquantity' },
        { label: '存货过账数量', prop: 'cHquantity' },
        { label: '即时库存剩余数', prop: 'inVquantity' },
        { label: '存货成本价', prop: 'fprice' },
        { label: this.$t('market.putStoageDate'), prop: 'inStockDate' },
        { label: '库龄', prop: 'kl' },
        { label: '库龄区间', prop: 'klqj' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = ['类型',	'业务区域', '销售单号', this.$t('market.paezText1'), '客户名称', '业务员', '型号',	'是否套件', '物料号',	'名称',	'描述',
          '销售订单数量',	'累计出库数',	'存货过账数量',	'即时库存剩余数',	'存货成本价',	'入库日期',	'库龄',	'库龄区间']
        const filterVal = ['lx',	'salCY', 'soNum', 'khdd', 'kh', 'saler', 'xh',	'tz', 'itemcode',	'fName',	'itemName',
          'salquantity',	'cKquantity',	'cHquantity',	'inVquantity',	'fprice',	'inStockDate',	'kl',	'klqj']
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          INVENTORY_info({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '存货过账表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取销售毛利列表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = { pageNum: this.currentPage, pageSize: this.size, search: this.searCollData.concat(this.details) }
      const { data: RES, total: total } = await INVENTORY_info(DATA)
      this.tableData = RES
      this.tableData.map(item => {
        item.salquantity = maxDecimal(item.salquantity)
        item.cKquantity = maxDecimal(item.cKquantity)
        item.cHquantity = maxDecimal(item.cHquantity)
        item.inVquantity = maxDecimal(item.inVquantity)
        item.fprice = maxDecimal(item.fprice)
      })
      this.total = total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  @include listBom;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
